<?php
	namespace Common\Model;
	/**
	 * 生成多层树状下拉选框的工具模型
	 */
	 class TreeModel{
		 //用于树型数组完成递归格式的全局变量
		 private $formatTree;


		 private function _toFormatTree($list,$level=0,$title='title'){
			 foreach($list as $key=>$value){
				 $tmp_str=str_repeat("--",$level*2);
				 $tmp_str.="└";
				 $value['level'] = $level;
				 $value['title_show'] =$level==0?$value[$title]:$tmp_str.$value[$title];
				 if(!array_key_exists('_child',$value)){
					 array_push($this->formatTree,$value);
				 }else{
					$tmp_ary = $value['_child'];
					unset($value['_child']);
					array_push($this->formatTree,$value);
					 $this->_toFormatTree($tmp_ary,$level+1,$title);
				 }
			 }
			 
			return;
			 
		 }
		 public function toFormatTree($list,$title = 'title',$pk='id',$pid = 'pid',$root = 0){
			$list = list_to_tree($list,$pk,$pid,'_child',$root);
			$this->formatTree=array();
			$this->_toFormatTree($list,0,$title);
			return $this->formatTree;
			
		}
		


	 }

?>